package com.zzsy.carosshopprojects.activitys.home.mainpage.member.memberconsum;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.activitys.home.mainpage.member.memberconsum.order.OrderDetailFragment;
import com.zzsy.carosshopprojects.adapter.ChoseOilAdapter;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.base.ConfigureFile;
import com.zzsy.carosshopprojects.bean.GetOilsListBean;
import com.zzsy.carosshopprojects.bean.MemberResultBean;
import com.zzsy.carosshopprojects.util.StringUtils;
import com.zzsy.carosshopprojects.util.ThrottleFirstUtil;
import com.zzsy.carosshopprojects.util.ViewBgUtil;
import com.zzsy.carosshopprojects.view.MyGridView;
import com.zzsy.carosshopprojects.view.MyNumberKeyboard;
import com.zzsy.carosshopprojects.view.dialog.ChoseOilDialog;
import com.zzsy.carosshopprojects.view.keyboard.AmountInputView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindDimen;
import butterknife.BindView;

/**
 * 会员消费
 */
public class MemberConsumFragment extends BaseFragment {
    public static MemberConsumFragment newInstance(String uuid) {
        MemberConsumFragment memberConsumFragment = new MemberConsumFragment();
        Bundle bundle = new Bundle();
        bundle.putString("uuid", uuid);
        memberConsumFragment.setArguments(bundle);
        return memberConsumFragment;
    }

    @BindView(R.id.recyclerView)
    MyGridView recycleView;
    @BindView(R.id.tv_name)
    TextView tvMmemberName;
    @BindView(R.id.tv_phone_member)
    TextView tvPhone;
    @BindView(R.id.tv_yue)
    TextView tvYue;
    @BindView(R.id.amountInputView)
    AmountInputView amountInputView;
    @BindView(R.id.keyboard)
    MyNumberKeyboard numberKeyboard;
    @BindView(R.id.ly_input)
    LinearLayout ly_input;
    @BindView(R.id.ly_show)
    LinearLayout ly_show;
    private ChoseOilAdapter choseOilAdapter;
    private String usedOil, productId;
    private String oilsPrice;
    private String productInfoId;
    private String memberOilsType;
    private String isUseCoupon;
    private List<GetOilsListBean> result;
    private List<GetOilsListBean> resultAll;
    @BindDimen(R.dimen.p_25)
    int p_20;
    private MemberConsumMode memberConsumMode;
    private MemberResultBean memberResultBean;

    @Override
    protected int initLayout() {
        return R.layout.fg_memberconsum;
    }

    @Override
    protected void initView(View view) {
        result = new ArrayList<>();
        resultAll = new ArrayList<>();
        initTitle(view, getResources().getString(R.string.MemberConsum), getActivity());
        numberKeyboard.setKeyClickCallback(new MyNumberKeyboard.KeyClickCallback() {
            @Override
            public void onNumClick(String keyNum) {
                amountInputView.addText(keyNum + "");
            }

            @Override
            public void onDelClick() {
                amountInputView.delLast();
            }

            @Override
            public void onCleanClick() {
                amountInputView.clean();
            }

            @Override
            public void sure() {
                if (ThrottleFirstUtil.isFastClick()) {
                    return;
                }
                String amount = (String) amountInputView.getAmountText();
                if ("0.".equals(amount) || "0.0".equals(amount) || "0.00".equals(amount) || "0".equals(amount) || StringUtils.isEmpty(amount)) {
                    ToastUtils.showToast(getContext().getApplicationContext(), getContext().getApplicationContext().getResources().getString(R.string.input_money));
                    return;
                }
                if (StringUtils.isEmpty(usedOil) || StringUtils.isEmpty(productId) || StringUtils.isEmpty(oilsPrice)) {
                    ToastUtils.showToast(getContext().getApplicationContext(), getContext().getApplicationContext().getResources().getString(R.string.chose_oil));
                    return;
                }
                if (memberOilsType.equals(productId)) {
                    isUseCoupon = "0";
                } else {
                    isUseCoupon = "1";
                }
                toOrderData(amount, isUseCoupon);
            }
        });
        ly_input.setBackground(ViewBgUtil.setWidthBg(15, 1, Color.parseColor(ConfigureFile.ColorWhite), Color.parseColor("#d7d7d7")));
        /**
         * 展示全部
         */
        ly_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (resultAll != null && resultAll.size() > 5) {
                    ChoseOilDialog choseOilDialog = new ChoseOilDialog(getActivity(), resultAll, new ChoseOilDialog.OnClickLister() {
                        @Override
                        public void select(int p) {
                            Collections.swap(resultAll, 0, p);
                            result.clear();
                            for (int i = 0; i < resultAll.size(); i++) {
                                if (i < 6) {
                                    result.add(resultAll.get(i));
                                }
                            }
                            choseOilAdapter.setListData(result);
                            choseOilAdapter.setPostion(0);
                            usedOil = result.get(0).getPnumber();
                            productId = result.get(0).getProductId();
                            oilsPrice = result.get(0).getPrice();
                            productInfoId = result.get(0).getProductInfoId();
                        }
                    });
                    choseOilDialog.show();
                } else {
                    ToastUtils.showToast(getContext().getApplicationContext(), "没有更多的油品了!");
                }

            }
        });
    }

    @Override
    protected void initData(Context mContext) {
        memberConsumMode = getViewModelProvider().get(MemberConsumMode.class);
        choseOilAdapter = new ChoseOilAdapter(getContext(), null, 0, true);

        recycleView.setAdapter(choseOilAdapter);

        recycleView.setOnItemClickListener((adapterView, view, position, l) -> {
            choseOilAdapter.setPostion(position);
            usedOil = choseOilAdapter.getListData().get(position).getPnumber();
            productId = choseOilAdapter.getListData().get(position).getProductId();
            oilsPrice = choseOilAdapter.getListData().get(position).getPrice();
            productInfoId = choseOilAdapter.getListData().get(position).getProductInfoId();
        });
    }

    @Override
    protected void loadMethod() {
        getUUIDMemberData();
        getOilData();
    }


    /**
     * 跳转到订单页
     */
    private void toOrderData(String amount, String isUseCoupon) {
        start(OrderDetailFragment.newInstance(memberResultBean.getMemberId(), amount, usedOil, productId, memberResultBean.getPassword(), memberResultBean.getPaySwitch(), oilsPrice, "4", memberResultBean.getRechargeBalance(), memberResultBean.getMemberCardId(), productInfoId, memberResultBean.getMemberType(), memberResultBean.getParentId(), oilsPrice, isUseCoupon));
    }


    /**
     * 根据UUID获取信息
     */
    private void getUUIDMemberData() {
        memberConsumMode.getLive().observe(this, memberResultBean -> {
            tvMmemberName.setText(memberResultBean.getRealName());
            tvPhone.setText(memberResultBean.getPhone());
            tvYue.setText("余额：￥" + memberResultBean.getRechargeBalance());
            memberOilsType = memberResultBean.getMemberOilsType();
            this.memberResultBean = memberResultBean;
        });
        memberConsumMode.MemberByUuid(getActivity(), getArguments().getString("uuid"));
    }

    /**
     * 处理油品列表数据
     */
    private void getOilData() {
        memberConsumMode.getLiveData().observe(this, getOilsListBeans -> {
            if (getOilsListBeans == null) {
                ToastUtils.showToast(getContext(), "数据返回异常");
                goBack(getActivity());
                return;
            }
            resultAll = getOilsListBeans;
            for (int i = 0; i < getOilsListBeans.size(); i++) {
                if (i < 6) {
                    result.add(getOilsListBeans.get(i));
                }
            }
            choseOilAdapter.setListData(result);
            choseOilAdapter.notifyDataSetChanged();
        });
    }
}
